package jointpart.assignment4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import shared.WordList;

public class SearchCmd4 {
	
	
	/**
	 * Programming Workshop assignment 4
	 *
	 * Reads textfile consisting of urls and words
	 * Stores each line in a hashtable by calling hashMapFile in Searcher.java
	 * Prompts user for a word to search and returns urls in which word appears
	 * 
	 * @param args - textfile
	 * 
	 */
	public static void main(String[] args) throws IOException {
		
		String name; // Filename
		
		if (args.length < 1 ) {
			System.out.println("Usage: java <datafile>");
			System.exit(1);
		}
		
		// Create hashtable from textfile
		WordList[] searchMap = Searcher4.hashMapFile(args[0], 200);
		
		if (args.length > 1){ // outputfile argument exists, run benchmark
        	
	        // create new benchmark test and run it
	        Benchmark4 myBenchmark = new Benchmark4();
	        myBenchmark.runBenchmark(searchMap, args[0], args[1]);
	        }
		
		// Prompt user for input
        BufferedReader inuser = new BufferedReader (new InputStreamReader (System.in));
        System.out.println ("Hit return to exit.");
        
        boolean quit = false; 
        
        String result = ""; // string holding result of search
            
        // Prompt user for word to search until quit
        while (!quit) {
            System.out.print ("Search for: ");
            name = inuser.readLine(); // Read a line from the terminal
              
	            if (name == null || name.length() == 0) {
	                quit = true;
	            }
	            
	            else {
	            	// Search for word in map
	            	result = Searcher4.getUrls(searchMap, name);
	            	
	            	// Print out results if any
	            	if (result != null) {
	            		System.out.println ("The word \""+ name+"\" has been found on: \n" + Searcher4.getUrls(searchMap, name));
	            	} 
	            	
	            	// if no search result
	            	else {
	            		System.out.println ("The word \""+ name+"\" has NOT been found.");
	            	}
	            }
	            
            }
            
	}

}
